module.exports = function deviceDetect(ua) {
  var os = this.os = {};
  var browser = this.browser = {};
  var webkit = ua.match(/Web[kK]it[\/]{0,1}([\d.]+)/); //返回一个匹配的字符串的数组
  var android = ua.match(/(Android);?[\s\/]+([\d.]+)?/);
  var ipad = ua.match(/(iPad).*OS\s([\d_]+)/);
  var ipod = ua.match(/(iPod)(.*OS\s([\d_]+))?/);
  var iphone = !ipad && ua.match(/(iPhone\sOS)\s([\d_]+)/);
  var webos = ua.match(/(webOS|hpwOS)[\s\/]([\d.]+)/);
  var touchpad = webos && ua.match(/TouchPad/);
  var kindle = ua.match(/Kindle\/([\d.]+)/);
  var silk = ua.match(/Silk\/([\d._]+)/);
  var blackberry = ua.match(/(BlackBerry).*Version\/([\d.]+)/);
  var bb10 = ua.match(/(BB10).*Version\/([\d.]+)/);
  var rimtabletos = ua.match(/(RIM\sTablet\sOS)\s([\d.]+)/);
  var playbook = ua.match(/PlayBook/);
  var chrome = ua.match(/Chrome\/([\d.]+)/) || ua.match(/CriOS\/([\d.]+)/);
  var firefox = ua.match(/Firefox\/([\d.]+)/);
  var ie = ua.match(/MSIE ([\d.]+)/);
  var safari = webkit && ua.match(/Mobile\//) && !chrome;
  var webview = ua.match(/(iPhone|iPod|iPad).*AppleWebKit(?!.*Safari)/) && !chrome;
  var ie = ua.match(/MSIE\s([\d.]+)/);
  
  
  if (browser.webkit = !!webkit) browser.version = webkit[1];
  
  if (android) os.android = true, os.version = android[2];
  if (iphone && !ipod) os.ios = os.iphone = true, os.version = iphone[2].replace(/_/g, '.');
  if (ipad) os.ios = os.ipad = true, os.version = ipad[2].replace(/_/g, '.');
  if (ipod) os.ios = os.ipod = true, os.version = ipod[3] ? ipod[3].replace(/_/g, '.') : null;
  if (webos) os.webos = true, os.version = webos[2];
  if (touchpad) os.touchpad = true;
  if (blackberry) os.blackberry = true, os.version = blackberry[2];
  if (bb10) os.bb10 = true, os.version = bb10[2];
  if (rimtabletos) os.rimtabletos = true, os.version = rimtabletos[2];
  if (playbook) browser.playbook = true;
  if (kindle) os.kindle = true, os.version = kindle[1];
  if (silk) browser.silk = true, browser.version = silk[1];
  if (!silk && os.android && ua.match(/Kindle Fire/)) browser.silk = true;
  if (chrome) browser.chrome = true, browser.version = chrome[1];
  if (firefox) browser.firefox = true, browser.version = firefox[1];
  if (ie) browser.ie = true, browser.version = ie[1];
  if (safari && (ua.match(/Safari/) || !!os.ios)) browser.safari = true;
  if (webview) browser.webview = true;
  if (ie) browser.ie = true, browser.version = ie[1];
  
  os.tablet = !!(ipad || playbook || (android && !ua.match(/Mobile/)) ||
  (firefox && ua.match(/Tablet/)) || (ie && !ua.match(/Phone/) && ua.match(/Touch/)));
  os.phone = !!(!os.tablet && !os.ipod && (android || iphone || webos || blackberry || bb10 ||
  (chrome && ua.match(/Android/)) || (chrome && ua.match(/CriOS\/([\d.]+)/)) ||
  (firefox && ua.match(/Mobile/)) || (ie && ua.match(/Touch/))));
  
  return {
  browser: browser,
  os: os
  };
  }
 
  // var detectObj =  detect(navigator.userAgent);